package com.jhws.business.appconfig.api;

import com.alibaba.fastjson.JSON;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.business.appconfig.bo.AppConfigContentInfo;
import com.jhws.business.appconfig.bo.AppConfigInfo;
import com.jhws.business.appconfig.service.AppConfigInfoService;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/v1/appConfig")
public class AppConfigV1 {

    @Resource
    private AppConfigInfoService appConfigInfoService;

    /**
     * 获取门口机，室内机配置
     *
     * @return
     */
    @SuppressWarnings("Duplicates")
    @RequestMapping("anon/getAppConfig")
    @ResponseBody
    public JsonRespData getAppConfig(String deviceType) {
        List<Map> retMaps = new ArrayList<>();
        List<AppConfigInfo> appConfigInfos = appConfigInfoService.queryAppConfigInfoByWhere("where device_type='" + deviceType + "'");
        Map map;
        for (AppConfigInfo appConfigInfo : appConfigInfos){
            map = new HashMap();
            map.put("id", appConfigInfo.getId());
            map.put("name", appConfigInfo.getName());
            map.put("type", appConfigInfo.getType());
            map.put("description", appConfigInfo.getDescription());
            if (StringUtils.isNotEmpty(appConfigInfo.getContent())){
                List<AppConfigContentInfo> appConfigContentInfos = JSON.parseArray(appConfigInfo.getContent(), AppConfigContentInfo.class);
                for (AppConfigContentInfo appConfigContentInfo : appConfigContentInfos){
                    map.put(appConfigContentInfo.getTypeKey(), appConfigContentInfo.getTypeValue());
                }
            }
            retMaps.add(map);
        }
        return new JsonRespData("", 1, retMaps);
    }
}
